package org.example.erp.controller;
import org.example.erp.domain.SysDept;
import org.example.erp.service.impl.SysDeptServiceImpl;
import org.example.erp.service.impl.SysRoleDeptServiceImpl;
import org.example.erp.service.impl.SysUserDeptServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;

@RequestMapping("/api")
@RestController
public class DeptController {

    @Autowired
    SysDeptServiceImpl sysDeptService;//岗位表

    @Autowired
    SysUserDeptServiceImpl sysUserDeptService;//用户岗位表

//     查询部门列表方法
    @GetMapping("/departmentManagement/find")
    public DataResult findDeptList(
            @RequestParam(required = false) String deptName,
            @RequestParam(required = false) Integer status)
    {
        List<SysDept> deptList = sysDeptService.findDeptList(deptName, status);
        return DataResult.success(deptList);
    }

    //查询部门是否分配角色
    @PostMapping("/departmentManagement/findBy_deptId")
    public DataResult findBy_deptId(@RequestBody List<Integer> ids){
        int byDeptId = sysUserDeptService.findBy_deptId(ids);
        return DataResult.success(byDeptId);
    }

    // 添加部门
    @PostMapping("/departmentManagement/insert")
    public DataResult insertDept(@RequestBody SysDept sysDept) {
        sysDept.setCreateTime(new Date());
        boolean save = sysDeptService.save(sysDept);
        if (save){
            return DataResult.success(save ? "成功" :"失败");
        } else{
            return DataResult.error("部门添加失败");
        }

    }

    // 修改部门
    @PutMapping("/departmentManagement/update")
    public DataResult updateDept(@RequestBody SysDept sysDept) {
        sysDept.setUpdateTime(new Date());
        boolean b = sysDeptService.updateById(sysDept);
        if (b){
            return DataResult.success(b ? "成功" : "失败");
        } else {
            return DataResult.error("部门更新失败");
        }
    }

    // 根据id删除部门
    @DeleteMapping("/departmentManagement/delete")
    public DataResult deleteDept(@RequestBody List<Integer> deptId) {
        sysDeptService.removeByIds(deptId);
        return DataResult.success();
    }

}
